package com.a2.appa3.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.a2.appa3.R;
import com.a2.appa3.global.HttpAdress;
import com.a2.appa3.global.Info;
import com.a2.appa3.model.CustomObserver;
import com.a2.appa3.model.network.HttpJsonUtils;
import com.a2.appa3.model.network.entity.req.Resaddfriend;
import com.a2.appa3.model.network.entity.req.Reschatlist;
import com.a2.appa3.model.network.entity.req.Resfindfriend;
import com.a2.appa3.util.OnItemClickListener;
import com.a2.appa3.util.SYSOUtils;

import java.util.ArrayList;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import me.shihao.library.XStatusBarHelper;


public class NewfriendActivity extends BaseActivitySimple {

    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.headerTitle)
    TextView headerTitle;
    @BindView(R.id.headerButton)
    ImageView headerButton;
    @BindView(R.id.headerButtonRoot)
    LinearLayout headerButtonRoot;
    @BindView(R.id.friendlist)
    RecyclerView friendlist;
    @BindView(R.id.friendlist1)
    RecyclerView friendlist1;
    @BindView(R.id.search_viewed)
    EditText searchViewed;
    @BindView(R.id.search_viewbt)
    TextView searchViewbt;
    @BindView(R.id.search_view)
    LinearLayout searchView;
    private FrAdapter adapter;
    private FrAdapter1 adapter1;
    private ArrayList<Reschatlist.FriendsLogListBean> frlist = new ArrayList<>();
    private ArrayList<Resfindfriend.DataBean> frlist1 = new ArrayList<>();
    //    private SearchView searchView;
    private Boolean flag = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        XStatusBarHelper.tintStatusBar(this, getResources().getColor(R.color.settingbg));
//        headerButton.setImageResource(R.mipmap.search);
        headerButton.setVisibility(View.INVISIBLE);
        searchView.setVisibility(View.VISIBLE);
        flag = false;

//        headerButton.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (flag) {
//                    searchView.setVisibility(View.VISIBLE);
//                    flag = false;
//                } else {
//                    friendlist.setVisibility(View.VISIBLE);
//                    friendlist1.setVisibility(View.GONE);
//                    frlist.clear();
//                    frlist.addAll(Info.newfriendlist);
//                    searchView.setVisibility(View.GONE);
//                    adapter.notifyDataSetChanged();
//                    flag = true;
//                }
//            }
//        });
        searchViewbt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(searchViewed.getText().toString().trim())) {
                    searchfriend(searchViewed.getText().toString().trim());
                } else {
                    Toast.makeText(NewfriendActivity.this, "您未输入任何信息，小子找不到呀！", Toast.LENGTH_SHORT).show();
                }
            }
        });
//
        // 5. 设置        searchViewbt.setOnClickSearch(new ICallBack() {
//            @Override
//            public void SearchAciton(String string) {
//                if (!TextUtils.isEmpty(string)) {
//                    searchfriend(string);
//                } else {
//                    Toast.makeText(NewfriendActivity.this, "您未输入任何信息，小子找不到呀！", Toast.LENGTH_SHORT).show();
//                }
//            }
//        });点击返回按键后的操作（通过回调接口）
//        searchView.setOnClickBack(new bCallBack() {
//            @Override
//            public void BackAciton() {
//                friendlist.setVisibility(View.VISIBLE);
//                friendlist1.setVisibility(View.GONE);
//                frlist.clear();
//                frlist.addAll(Info.newfriendlist);
//                adapter.notifyDataSetChanged();
//                searchView.setVisibility(View.GONE);
//                flag = true;
//            }
//        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        initdata();
        headerTitle.setText("新的好友");
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(this);
        friendlist.setLayoutManager(linearLayoutManager);
        friendlist1.setLayoutManager(linearLayoutManager1);
        adapter = new FrAdapter();
        adapter1 = new FrAdapter1();

        friendlist.setAdapter(adapter);
        friendlist1.setAdapter(adapter1);

    }

    private void searchfriend(String str) {
        HttpJsonUtils.getHttpApiInstance()
                .finfriend(HttpAdress.getuseAdress() + "/api/chatapi/User/searchUser", str, Info.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<Resfindfriend>() {
                    @Override
                    protected void onCustomNext(Resfindfriend resaddfriend) {
                        if (resaddfriend.data.size() != 0) {
                            SYSOUtils.sOut_("soudaosou搜到" + resaddfriend.data.toString());
                            friendlist.setVisibility(View.GONE);
                            friendlist1.setVisibility(View.VISIBLE);
                            frlist1.clear();
                            frlist1.addAll(resaddfriend.data);
                            adapter1.notifyDataSetChanged();
                        } else {
                            Toast.makeText(NewfriendActivity.this, "没查询到您所搜索的信息", Toast.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {

                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        Toast.makeText(NewfriendActivity.this, "++++++++++++++++", Toast.LENGTH_LONG).show();
                    }
                });
    }

    private void initdata() {
        frlist.clear();
        frlist.addAll(Info.newfriendlist);
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_newfriend;
    }

    class FrAdapter extends RecyclerView.Adapter<FrAdapter.MViewHolder>
            implements OnItemClickListener {
        @Override
        public MViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MViewHolder(LayoutInflater.from(NewfriendActivity.this)
                    .inflate(R.layout.nfr_item, parent, false));
        }


        @Override
        public void onBindViewHolder(final MViewHolder holder, final int position) {
            Glide.with(NewfriendActivity.this).load(HttpAdress.getuseAdress() +
                    R.mipmap.default_head).error(R.mipmap.default_head).into(holder.userimg);
            holder.chat_name.setText(frlist.get(position).name + "");
            if (frlist.get(position).status == 0) {
                holder.acceptbtlayout.setVisibility(View.VISIBLE);
                holder.tongyi.setVisibility(View.GONE);
            } else if (frlist.get(position).status == 1) {
                holder.acceptbtlayout.setVisibility(View.GONE);
                holder.tongyi.setVisibility(View.VISIBLE);
                holder.tongyi.setText("已同意");
            } else {
                holder.acceptbtlayout.setVisibility(View.GONE);
                holder.tongyi.setVisibility(View.VISIBLE);
                holder.tongyi.setText("已拒绝");
            }
            holder.acceptbt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    addfriend(frlist.get(position).id + "", 1 + "");
                }
            });
            holder.noacceptbt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    addfriend(frlist.get(position).id + "", 2 + "");
                }
            });
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(NewfriendActivity.this, SearchActivity.class);
                    intent.putExtra("username", frlist.get(position).user_id + "");
                    intent.putExtra("usertype", "users");
                    startActivity(intent);
                    finish();
                }
            });
        }

        @Override
        public int getItemCount() {
            return frlist.size();

        }

        @Override
        public void onItemClick(View view, int position) {

        }

        class MViewHolder extends RecyclerView.ViewHolder {
            TextView acceptbt, chat_name, noacceptbt, tongyi;
            LinearLayout acceptbtlayout;
            ImageView userimg;

            MViewHolder(View itemView) {
                super(itemView);
                acceptbtlayout = (LinearLayout) itemView.findViewById(R.id.acceptbtlayout);
                acceptbt = (TextView) itemView.findViewById(R.id.acceptbt);
                chat_name = (TextView) itemView.findViewById(R.id.chat_name);
                noacceptbt = (TextView) itemView.findViewById(R.id.noacceptbt);
                tongyi = (TextView) itemView.findViewById(R.id.tongyi);
                userimg = (ImageView) itemView.findViewById(R.id.user_img);
            }
        }
    }


    /////////////////////////////////////////////////搜到的好友///////////////////////////////////////////////////////////
    class FrAdapter1 extends RecyclerView.Adapter<FrAdapter1.MViewHolder>
            implements OnItemClickListener {
        @Override
        public MViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MViewHolder(LayoutInflater.from(NewfriendActivity.this)
                    .inflate(R.layout.newfrienditem, parent, false));
        }


        @Override
        public void onBindViewHolder(final MViewHolder holder, final int position) {
            Glide.with(NewfriendActivity.this).load(HttpAdress.getuseAdress() +
                    R.mipmap.default_head).error(R.mipmap.default_head).into(holder.chatimge);
            holder.timetext.setVisibility(View.INVISIBLE);
            holder.chat_name.setText(frlist1.get(position).name + "");
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(NewfriendActivity.this, SearchActivity.class);
                    intent.putExtra("username", frlist1.get(position).id + "");
                    intent.putExtra("groundid", frlist1.get(position).name + "");
                    intent.putExtra("usertype", frlist1.get(position).type + "");
                    startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return frlist1.size();

        }

        @Override
        public void onItemClick(View view, int position) {

        }

        class MViewHolder extends RecyclerView.ViewHolder {
            TextView acceptbt, chatcontent, chat_name, timetext;
            ImageView chatimge;

            MViewHolder(View itemView) {
                super(itemView);
                chatimge = (ImageView) itemView.findViewById(R.id.user_img);
                acceptbt = (TextView) itemView.findViewById(R.id.acceptbt);
                chat_name = (TextView) itemView.findViewById(R.id.chat_name);
                timetext = (TextView) itemView.findViewById(R.id.timetext);
                chatcontent = (TextView) itemView.findViewById(R.id.chatcontent);
            }
        }
    }

    private void addfriend(String user_id, String i) {
        HttpJsonUtils.getHttpApiInstance()
                .addfriend(HttpAdress.getuseAdress() + "api/chatapi/User/handleUserFriendsLog", user_id, i, Info.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<Resaddfriend>() {
                    @Override
                    protected void onCustomNext(Resaddfriend resLogin) {
                        Toast.makeText(NewfriendActivity.this, "" + resLogin.getMsg(), Toast.LENGTH_SHORT).show();
                        frlist.clear();
                        frlist.addAll(Info.newfriendlist);
                        adapter.notifyDataSetChanged();
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {

                    }
                });
    }
}
